package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Product;
import com.yh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit, HttpServletRequest request){
        PageHelper.startPage(page, limit);
        List<Product> products = productService.findAll();
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        //request.setAttribute("products",products);
        request.setAttribute("pageInfo",pageInfo);
        return "/product/product-list";
    }
    @RequestMapping("/show")
    public String findAll(int id,HttpServletRequest request){

        Product product = productService.findById(id);

        request.setAttribute("product",product);
        return "/product/product-show";
    }



    @RequestMapping("/json")
    @ResponseBody
    public  PageInfo<Product> json(@RequestParam(value = "page",required = false,defaultValue = "1") int page, @RequestParam(value = "limit",required = false,defaultValue = "5")int limit, HttpServletRequest request){
        PageHelper.startPage(page, limit);
        List<Product> products = productService.findAll();
        PageInfo<Product> pageInfo=new PageInfo<>(products);
        return pageInfo;
    }

}
